using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;

namespace BookBarter
{
    [TestFixture]
    public class UnitTestsDB
    {
		#region Person 
		//Test CreatePerson
		[Test]
		public void TestCreatePerson()
		{
            String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
			Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
            Assert.AreEqual(newperson.UserId, userId);
		}

		//Test GetPerson (with Valid ID)
		[Test]
		public void TestGetPerson()
		{
            String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
			Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
            Person p = Person.GetPerson(newperson.UserId);
			Assert.AreEqual(p, newperson);
		}

        //Test Save
        [Test]
        public void TestPersonSave()
        {
            String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
            Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
            newperson.LastName = "someting else";
			Person p = Person.GetPerson(newperson.UserId);
			Assert.AreEqual(p.LastName, "something else");
        }

		//Test GetPerson (with Invalid ID)
		[Test]
		[ExpectedException(typeof(NoSuchPerson))]
		public void TestGetInvalidTradePerson()
		{
			Person p = Person.GetPerson("THISISAFAKEID");
		}

		//Test AddBookWant()
		[Test]
		public void TestAddBookWant()
		{
			String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
			Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
			Book bk = Book.CreateBook("Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel", 981, "images/lotr.png");
			BookWant bw = BookWant.CreateBookWant(newperson, bk, Condition.Excellent);
		    newperson.addBookWant(bw);
		    Assert.IsTrue(newperson.BookWants.Contains(bw));
		}

		////Test AddBookHave()
		//[Test]
		public void TestAddBookHave()
		{
			String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
			Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
			Book bk = Book.CreateBook("Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel", 981, "images/lotr.png");
			BookHave bh = BookHave.CreateBookHave(newperson, bk, Condition.Excellent);
			newperson.addBookHave(bh);
			Assert.IsTrue(newperson.BookHaves.Contains(bh));
		}
		#endregion

        #region Book
		//Test CreateBook
		[Test]
		public void TestCreateBook()
		{
		    Book newbook = Book.CreateBook( "Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel",981,"images/lotr.png");
		    Assert.IsNotNull(newbook);
		}

        //Test GetBook (with Valid ID)
        [Test]
        public void TestGetBook()
        {
            Book newbook = Book.CreateBook("Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel", 981, "images/lotr.png");
            Book b = Book.GetBook(newbook.ID);
            Assert.AreEqual(b,newbook);
        }

		//Test Save
		[Test]
		public void TestBookSave()
		{
		    Book newbook = Book.CreateBook("Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel", 981, "images/lotr.png");
		    newbook.ImageUrl = "something else";
			Book b = Book.GetBook(newbook.ID);
			Assert.AreEqual(b.ImageUrl, "something else");
		}

        ////Test GetBook (with Invalid ID)
        [Test]
        [ExpectedException(typeof(NoSuchBook))]
        public void TestGetInvalidBook()
        {
            Book tb = Book.GetBook(new Guid("000000000000000000000000000000000000"));
        }
        #endregion

        #region BookHave
		//Test CreateBookHave
		[Test]
		public void TestCreateBookHave()
		{
			String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
			Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
			Book bk = Book.CreateBook("Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel", 981, "images/lotr.png");
			BookHave bh = BookHave.CreateBookHave(newperson, bk, Condition.Excellent);
			Assert.IsNotNull(bh);
		}
		
		////Test GetBookHave (with Valid ID)
        [Test]
        public void TestGetBookHave()
        {
			String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
			Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
			Book bk = Book.CreateBook("Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel", 981, "images/lotr.png");
			BookHave bh = BookHave.CreateBookHave(newperson, bk, Condition.Excellent);
			BookHave bh2 = BookHave.GetBookHave(bh.ID);
            Assert.AreEqual(bh,bh2);
        }

        ////Test GetBookHave (with Invalid ID)
        //[Test]
        [ExpectedException(typeof(NoSuchBookHave))]
        public void TestGetInvalidBookHave()
        {
            BookHave bh = BookHave.GetBookHave(new Guid("00000000000000000000000000000000000"));
        }
        #endregion

        #region BookWant
		//Test CreateBookWant
		[Test]
		public void TestCreateBookWant()
		{
			String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
			Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
			Book bk = Book.CreateBook("Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel", 981, "images/lotr.png");
			BookWant bw = BookWant.CreateBookWant(newperson, bk, Condition.Excellent);
			Assert.IsNotNull(bw);
		}

		////Test GetBookWant (with Valid ID)
		[Test]
		public void TestGetBookWant()
		{
			String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
			Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
			Book bk = Book.CreateBook("Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel", 981, "images/lotr.png");
			BookWant bw = BookWant.CreateBookWant(newperson, bk, Condition.Excellent);
			BookWant bw2 = BookWant.GetBookWant(bw.ID);
			Assert.AreEqual(bw, bw2);
		}

		////Test GetBookWant (with Invalid ID)
		//[Test]
		[ExpectedException(typeof(NoSuchBookWant))]
		public void TestGetInvalidBookWant()
		{
			BookWant bw = BookWant.GetBookWant(new Guid("00000000000000000000000000000000000"));
		}
        #endregion

        #region TradeRoute
		//Test CreateNewTradeRoute
		[Test]
		public void TestCreateNewTradeRoute()
		{
		    TradeRoute newtraderoute = TradeRoute.CreateNewTradeRoute();
		}

		//Test GetTradeRoute (with Valid ID)
        [Test]
        public void TestGetTradeRoute()
        {
			TradeRoute tr = TradeRoute.CreateNewTradeRoute();
            TradeRoute tr2 = TradeRoute.GetTradeRoute(tr.ID);
            Assert.AreEqual(tr.ID,tr2.ID);
        }

        //Test GetTradeRoute (with Invalid ID)
        [Test]
        [ExpectedException(typeof(NoSuchTradeRoute))]
        public void TestGetInvalidTradeRoute()
        {
            TradeRoute tr = TradeRoute.GetTradeRoute(new Guid("0000000000000000000000000000000000"));
        }

        //Test AddTradeLink()
        [Test]
        public void TestAddTradeLink()
        {
			String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
			Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
			Book bk = Book.CreateBook("Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel", 981, "images/lotr.png");
			BookWant bw = BookWant.CreateBookWant(newperson, bk, Condition.Excellent);
			BookHave bh = BookHave.CreateBookHave(newperson, bk, Condition.Excellent);
			TradeRoute tr = TradeRoute.CreateNewTradeRoute();
			TradeLink tl = tr.AddTradeLink(bh, bw);
			Assert.AreEqual(tr.LastLink, tl);
        }
        #endregion

        #region TradeLink
		//Test CreateNewTradeLink
		[Test]
		public void TestCreateNewTradeLink()
		{
			String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
			Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
			Book bk = Book.CreateBook("Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel", 981, "images/lotr.png");
			BookWant bw = BookWant.CreateBookWant(newperson, bk, Condition.Excellent);
			BookHave bh = BookHave.CreateBookHave(newperson, bk, Condition.Excellent);
			TradeRoute tr = TradeRoute.CreateNewTradeRoute();
			TradeLink tl = TradeLink.CreateNewTradeLink(tr, bh, bw);
			Assert.IsNotNull(tl);
		}

        //Test GetTradeLink (with Valid ID)
        [Test]
        public void TestGetTradeLink()
        {
			String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
			Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
			Book bk = Book.CreateBook("Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel", 981, "images/lotr.png");
			BookWant bw = BookWant.CreateBookWant(newperson, bk, Condition.Excellent);
			BookHave bh = BookHave.CreateBookHave(newperson, bk, Condition.Excellent);
			TradeRoute tr = TradeRoute.CreateNewTradeRoute();
			TradeLink tl = TradeLink.CreateNewTradeLink(tr, bh, bw);
			TradeLink tl2 = TradeLink.GetTradeLink(tl.ID, tl.Route);
            Assert.AreEqual(tl.ID,tl2.ID);
        }

        //Test GetTradeLink (with Invalid ID)
        [Test]
        [ExpectedException(typeof(NoSuchTradeLink))]
        public void TestGetInvalidTradeLink()
        {
			String userId = "jdh" + (new Random()).Next(1, 100000000).ToString();
			Person newperson = Person.CreatePerson("John", "Doe", userId, "jdh101@psu.edu");
			Book bk = Book.CreateBook("Lord of The Rings", "J.R.R. Tolkien", "Bantam", "en", "11111", "Classic Fantasy Novel", 981, "images/lotr.png");
			BookWant bw = BookWant.CreateBookWant(newperson, bk, Condition.Excellent);
			BookHave bh = BookHave.CreateBookHave(newperson, bk, Condition.Excellent);
			TradeRoute tr = TradeRoute.CreateNewTradeRoute();
            TradeLink tl = TradeLink.GetTradeLink(new Guid("000000000000000000000"), tr);
        }
        #endregion

    }
}
